package com.example.nbshopping;

import android.content.Intent;
import android.os.Bundle;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ListView;

import com.example.nbshopping.goods.GoodsTypeFragment;
import com.example.nbshopping.goods.QueryByNameMainActivity;
import com.example.nbshopping.goods.TypeAdapter;
import com.example.nbshopping.goods.TypeBean;
import com.example.nbshopping.utils.BaseFragment;
import com.example.nbshopping.utils.URLUtils;
import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.List;

public class TypeFragment extends BaseFragment {
    ListView typeLv;
    EditText searchEt;
    ImageButton searchBtn;
    List<TypeBean.DataBean> data;
    private TypeAdapter adapter;
    private FragmentManager fm;
    private String hint="JavaScript";
    private String name;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view= inflater.inflate(R.layout.fragment_type, container, false);
        initView(view);
        fm = getChildFragmentManager();
        //确定数据源
        data=new ArrayList<>();
        adapter=new TypeAdapter(getContext(),data);
        typeLv.setAdapter(adapter);
        //连接网络
        getNetwork(URLUtils.queryAllCategory_url);
        //
        setListener();
        return view;
    }
//点击左侧的item切换右侧的页面
    private void changeRightPage(int position) {
        TypeBean.DataBean dataBean = data.get(position);//得到切换的数据
        int id = dataBean.getId();
        FragmentTransaction transaction = fm.beginTransaction();
        GoodsTypeFragment gtfrag = GoodsTypeFragment.newInstance(id + "", "");
        transaction.replace(R.id.frag_type_layout,gtfrag);
        transaction.commit();
    }

    private void setListener() {
        typeLv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                adapter.setClickPos(position);
                changeRightPage(position);
            }
        });
        View.OnClickListener onClickListener = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(v.getId()==R.id.frag_type_btn){
                    Intent intent=new Intent(getContext(), QueryByNameMainActivity.class);
                    name=String.valueOf(searchEt.getText());
                    if(name.isEmpty()) {//判断搜索框中的数据是否为空
                        intent.putExtra("hint",hint);
                    }else {
                        intent.putExtra("name",name);
                    }
                    startActivity(intent);
                }
            }
        };
        searchBtn.setOnClickListener(onClickListener);
    }


    @Override
    public void onSuccess(String result) {
        super.onSuccess(result);
        TypeBean typeBean = new Gson().fromJson(result, TypeBean.class);
        List<TypeBean.DataBean> list=typeBean.getData();
        data.addAll(list);
        adapter.notifyDataSetChanged();//提示更新
        changeRightPage(0);
    }

    private void initView(View view) {
        typeLv=view.findViewById(R.id.frag_type_lv);
        searchEt=view.findViewById(R.id.frag_type_et);
        searchBtn=view.findViewById(R.id.frag_type_btn);
        searchEt.setHint(hint);
    }
}